package eip.chapter9.splitaggregate.impl;

import java.util.HashMap;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jibx.runtime.JiBXException;

import eip.chapter9.splitaggregate.AggregatedResponse;
import eip.chapter9.splitaggregate.InformationRequest;
import eip.util.JibxUtil;

public class InformationBroker {

	public final static Log LOG = LogFactory.getLog(InformationBroker.class);
	private static HashMap<String,AggregatedResponse> returnedQueries = new HashMap<String, AggregatedResponse>();
	
	public void processAggregatedResponse(AggregatedResponse response) throws JiBXException {
		LOG.info("Received aggregated response:" + response);
		LOG.info("As XML:" + JibxUtil.marshall(response));
		LOG.info("Storing response with id: " + response.getId());
		
		returnedQueries.put(response.getId(), response);
	}
	
	public Object bridge(InformationRequest obj) {
		LOG.info("Bridging object:" + obj);
		return obj;
	}
	
	public AggregatedResponse getResponse(String correlationId) {
		System.out.println("correlationID:" + correlationId);
		return returnedQueries.get(correlationId);
	}
}
